// const router = require("../routers/m1")
const Router = require("koa-router");
const requireDirectory = require("require-directory");
// requireDirectory(module,`${process.cwd()}/routers/m1`,{visit:loadRouters});
const render = require("koa-art-template")
const static = require("koa-static");
const koaBody = require("koa-body")//加

function initManage(app){
    app.use(koaBody({
        multipart:true, // 支持文件上传
        // encoding:'gzip',
        formidable:{
          uploadDir:`${process.cwd()}/static`, // 设置文件上传目录
          keepExtensions: true,    // 保持文件的后缀
          maxFieldsSize:20 * 1024 * 1024, // 文件上传大小
          /* onFileBegin:(name,file) => { // 文件上传前的设置
            // console.log(`name: ${name}`);
            // console.log(file);
        }, */
        }}));
    // app.use(router.routes())
    // 加载/routers/m1下的文件时，会触发loadRouters函数
    requireDirectory(module,`${process.cwd()}/routers`,{
        visit:loadRouters
    })
    render(app,{
        root:`${process.cwd()}/views`,
        extname:'.html',
        debug:process.env.NODE_ENV !== 'production'
    })
    function loadRouters(obj){
        console.log(obj);
        if(obj instanceof Router){
            app.use(obj.routes())
        }
    }
    app.use(static(`${process.cwd()}/images`))      //
    app.use(static(`${process.cwd()}/static`))      //
}
module.exports = initManage